import ApiError from "../error/ApiError";
import ApiErrorNames from "../error/ApiErrorNames";
import axios from "axios";
import shop from "../model/shop";
import DlyadminAiFuncsConfig from "../model/dlyadminAiFuncsConfig";
import JianceOrderModel from "../model/JianceOrderModel";
import redis from '../dbHelper/redis'
import mongoose from "mongoose";
import crypto from "crypto"
const AppKey = '202409261509475416'
const appSecret = 'ddf6748353855ddf03b53331d96509d0'

import HotTitle from '../model/hotTitleModel'


export default class HotTitleApiController {
    static async create(ctx) {
        console.log('ctx.request.body', ctx.request.body)
        // console.log('ctx.request.body', ctx.request.header.shop_id)
        const redisKey = `shopid_${ctx.request.header.shop_id}`
        const redisValue = await redis.get(redisKey)
        // console.log('redisValueredisValue', redisValue)
        if (redisValue) {
            try {
                const objJSON = JSON.parse(redisValue)
                const bodyJSON = JSON.parse(ctx.request.body)
                console.log('objJSON', bodyJSON)
                const gter = await HotTitle.updateOne({
                    shop_info: objJSON._id
                },
                    { $addToSet: { hot_title: { $each: bodyJSON } } }
                    // $push: {
                    //     hot_title: bodyJSON
                    // }
                    ,
                    { upsert: true }
                )
                console.log('objJSON', gter)

                return ctx.body = gter
            } catch (error) {
                return ctx.body = 'err'
            }
        }

    }

    static async list(ctx) {

        const redisKey = `shopid_${ctx.request.header.shop_id}`
        const redisValue = await redis.get(redisKey)
        if (redisValue) {
            try {
                const objJSON = JSON.parse(redisValue)
                const gter = await HotTitle.findOne({
                    shop_info: objJSON._id
                })
                return ctx.body = gter ? gter : 1
            } catch (error) {
                return ctx.body = 'err'
            }
        }
    }

    static async delete(ctx) {
        const redisKey = `shopid_${ctx.request.header.shop_id}`

        const redisValue = await redis.get(redisKey)

        if (redisValue) {
            try {
                const objJSON = JSON.parse(redisValue)
                const bodyJSON = JSON.parse(ctx.request.body)
                // console.log('objJSON', bodyJSON)
                const gter = await HotTitle.updateOne({
                    shop_info: objJSON._id
                },
                    { $pull: { hot_title: { $in: bodyJSON } } }
                    // $push: {
                    //     hot_title: bodyJSON
                    // }
                    ,
                    { upsert: true }
                )
                // console.log('objJSON', gter)

                return ctx.body = gter
            } catch (error) {
                return ctx.body = 'err'
            }
        }
    }


}


